﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets.MindMap
{
    public class TitleLinkAdorner : Adorner
    {
        /// <summary>
        /// 用以表示标题是对文档或图片的链接。
        /// </summary>
        /// <param name="adornedElement">应传入标题的“MainBorder”主子元素。</param>
        public TitleLinkAdorner(UIElement adornedElement, MindMapTitle masterTitle)
            : base(adornedElement)
        {
            this.masterTitle = masterTitle;

            //this.Opacity = 0.75;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            //this.SnapsToDevicePixels = true;
            //this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

        }

        private MindMapTitle masterTitle;

        public MindMapTitle MasterTitle
        {
            get { return masterTitle; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (this.Visibility != System.Windows.Visibility.Visible || masterTitle == null || masterTitle.MasterView == null) return;
            if (this.masterTitle.Element == null)
            {
                this.Visibility = Visibility.Collapsed;
                return;
            }

            double defaultFontSize = MasterTitle.MasterView.TitleDefaultFontSize;
            if (double.IsNaN(defaultFontSize))
            {
                defaultFontSize = (double)Application.Current.TryFindResource("title_default_font_size");
            }

            Brush brush = Brushes.White;
            Pen pen = new Pen(brush, 2);
            if (this.masterTitle != null && this.masterTitle.MasterView != null)
            {
                brush = this.masterTitle.MasterView.TitleDefaultActiveBrush;
                pen = new Pen(this.masterTitle.MasterView.TitleDefaultBackground, 2);
            }

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            //adornedElementRect.X -= 6; adornedElementRect.Y -= 6;
            //adornedElementRect.Width += 12; adornedElementRect.Height += 12;

            double halfHeight = adornedElementRect.Height / 2;
            //drawingContext.DrawRoundedRectangle(null, pen, adornedElementRect, halfHeight, halfHeight);

            // drawingContext.DrawRectangle(brush, null, new Rect(adornedElementRect.Right - defaultFontSize, adornedElementRect.Top - defaultFontSize / 2, defaultFontSize, defaultFontSize));
            //var imgSource = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/eiAnalys-2.png"));
            //drawingContext.DrawImage(imgSource, new Rect(adornedElementRect.Right - defaultFontSize, adornedElementRect.Top - defaultFontSize / 2, defaultFontSize, defaultFontSize));

            Geometry geometry;

            switch (this.masterTitle.Element.Type)
            {
                case TreeElementType.FileLink:
                    {
                        geometry = new EllipseGeometry(new Rect(adornedElementRect.Right - defaultFontSize, adornedElementRect.Bottom - defaultFontSize / 4, defaultFontSize / 2, defaultFontSize / 2));
                        drawingContext.DrawGeometry(brush, pen, geometry);
                        this.ToolTip = "文档链接";
                        break;
                    }
                case TreeElementType.ImageLink:
                    {
                        geometry = new RectangleGeometry(new Rect(adornedElementRect.Right - defaultFontSize, adornedElementRect.Bottom - defaultFontSize / 4, defaultFontSize / 2, defaultFontSize / 2));
                        drawingContext.DrawGeometry(brush, pen, geometry);
                        this.ToolTip = "图像链接";
                        break;
                    }
                default:
                    {
                        this.Visibility = Visibility.Collapsed;
                        this.ToolTip = null;
                        return;
                    }
            }
        }
    }
}
